package cn.broadsense.web.controller.biz;

import cn.broadsense.biz.domain.CollectDataTask;
import cn.broadsense.biz.domain.CollectDataTaskChannel;
import cn.broadsense.biz.service.CollectDataTaskChannelService;
import cn.broadsense.biz.service.CollectDataTaskService;
import cn.broadsense.biz.vo.CollectDataTaskAuditVo;
import cn.broadsense.biz.vo.CollectDataTaskVo;
import cn.broadsense.common.core.controller.BaseController;
import cn.broadsense.common.core.domain.R;
import cn.broadsense.common.core.domain.entity.SysUser;
import cn.broadsense.common.core.page.TableDataInfoResult;
import cn.broadsense.common.utils.DateUtils;
import cn.broadsense.framework.manager.AsyncManager;
import cn.broadsense.system.domain.SysNotice;
import cn.broadsense.system.service.ISysUserService;
import cn.broadsense.web.factory.AdminAsyncFactory;
import com.alibaba.fastjson2.JSONObject;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;

/**
 * 采集任务
 *
 * @author: pengxin
 * @create: 2023-09-12 15:31
 * @Version 1.0
 **/
@RestController
@Api(tags = "采集任务")
@RequestMapping("/collectDataTask")
public class CollectDataTaskController extends BaseController {
    @Resource
    private CollectDataTaskService dataTaskService;

    @Resource
    private CollectDataTaskChannelService dataTaskChannelService;

    @Resource
    private ISysUserService sysUserService;

    @PreAuthorize("@ss.hasPermi('biz:collectDataTask:add')")
    @PostMapping
    @ApiOperation("添加任务")
    public R addTask(@RequestBody @Validated CollectDataTaskVo collectDataTaskVo) {
        checkCollectUser(collectDataTaskVo);
        CollectDataTask collectDataTask = dataTaskService.addDataTask(collectDataTaskVo);
        if (collectDataTask != null) {
            SysNotice sysNotice = new SysNotice();
            sysNotice.setNoticeTitle("【任务派送】 您有一条新的任务需要处理");
            sysNotice.setNoticeType("3");
            sysNotice.setStatus("0");
            sysNotice.setRelationId(collectDataTask.getId());
            sysNotice.setRelationModule(1);
            sysNotice.setRelationContent(JSONObject.toJSONString(collectDataTask));
            sysNotice.setUserId(collectDataTask.getCollectUserId());
            sysNotice.setCreateTime(DateUtils.getNowDate());
            AsyncManager.me().execute(AdminAsyncFactory.recordNotice(sysNotice));
        }
        return R.ok();
    }

    @PreAuthorize("@ss.hasPermi('biz:collectDataTask:edit')")
    @PutMapping("/{id}")
    @ApiOperation("更新任务")
    public R updateTask(@PathVariable Long id, @RequestBody @Validated CollectDataTaskVo collectDataTaskVo) {
        checkCollectUser(collectDataTaskVo);
        collectDataTaskVo.setId(id);
        dataTaskService.update(collectDataTaskVo);
        return R.ok();
    }

    @PreAuthorize("@ss.hasPermi('biz:collectDataTask:audit')")
    @PutMapping("/audit")
    @ApiOperation("任务审批")
    public R audit(@RequestBody @Validated CollectDataTaskAuditVo collectDataTaskAuditVo) {
        dataTaskService.auditing(collectDataTaskAuditVo);
        CollectDataTask collectDataTask = dataTaskService.getDetails(collectDataTaskAuditVo.getId());
        if (collectDataTask != null) {
            SysNotice sysNotice = new SysNotice();
            sysNotice.setNoticeTitle("【审核通知】 您有一条任务已审核");
            sysNotice.setNoticeType("4");
            sysNotice.setStatus("0");
            sysNotice.setRelationId(collectDataTask.getId());
            sysNotice.setRelationModule(1);
            sysNotice.setRelationContent(JSONObject.toJSONString(collectDataTask));
            sysNotice.setUserId(collectDataTask.getCollectUserId());
            sysNotice.setCreateTime(DateUtils.getNowDate());
            AsyncManager.me().execute(AdminAsyncFactory.recordNotice(sysNotice));
        }
        return R.ok();
    }


    @PreAuthorize("@ss.hasPermi('biz:collectDataTask:list')")
    @GetMapping("/list")
    @ApiOperation("获取任务列表")
    public R<TableDataInfoResult<List<CollectDataTask>>> list(CollectDataTask collectDataTask) {
        List<CollectDataTask> list = dataTaskService.findPage(collectDataTask);
        return R.ok(getDataTableResult(list));
    }


    @PreAuthorize("@ss.hasPermi('biz:collectDataTask:details')")
    @GetMapping("/{id}")
    @ApiOperation("获取任务详情")
    public R<CollectDataTask> getDetails(@PathVariable Long id) {
        CollectDataTask details = dataTaskService.getDetails(id);
        return R.ok(details);
    }

    @PreAuthorize("@ss.hasPermi('biz:collectDataTask:details')")
    @GetMapping("/channelList/{taskId}")
    @ApiOperation("获取任务点位列表")
    public R<List<CollectDataTaskChannel>> taskChannelList(@PathVariable Long taskId) {
        List<CollectDataTaskChannel> dataTaskChannelList = dataTaskChannelService.listByTaskId(taskId);
        return R.ok(dataTaskChannelList);
    }


    @PreAuthorize("@ss.hasPermi('biz:collectDataTask:delete')")
    @DeleteMapping("/{id}")
    @ApiOperation("删除任务")
    public R delete(@PathVariable Long id) {
        dataTaskService.deleteById(id);
        return R.ok();
    }


    /**
     * 校验采集人员
     *
     * @param collectDataTaskVo
     */
    private void checkCollectUser(CollectDataTaskVo collectDataTaskVo) {
        SysUser sysUser = sysUserService.selectUserById(collectDataTaskVo.getCollectUserId());
        if (sysUser == null) {
            throw new SecurityException("采集人员不存在");
        }
        collectDataTaskVo.setCollectUserName(sysUser.getUserName());
        collectDataTaskVo.setCollectUserDeptId(sysUser.getDeptId());
        if (sysUser.getDept() != null) {
            collectDataTaskVo.setCollectUserDeptName(sysUser.getDept().getDeptName());
        }
    }

}
